package com.yjhb.android.sbz.mvp.withdrawal;

import com.alibaba.fastjson.JSONObject;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.entity.Order;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.math.BigDecimal;

/**
 * 创建者 zx
 * 创建时间 2020/12/4 16:37
 */
public class WithdrawalPresenter extends ConventionPresenter<WithdrawalView> {
    private final String TAG = "WithdrawalPresenter";

    public WithdrawalPresenter(WithdrawalView withdrawalView) {
        super(withdrawalView);
    }

    @Override
    protected void onDestroy() {

    }

    /**
     * 提现
     *
     * @param source
     * @param target
     * @param money
     */
    public void withdrawal(Integer source, Integer target, BigDecimal money) {
        if (view != null) {
            view.onWait();
        }
        HttpTool.request(model.withdrawal(source, target, money), new HttpObserver<Order>() {
            @Override
            public void onResult(Order data) {
                if (view != null) {
                    view.onSuccess(data);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(-2, msg);
                }
            }
        });
    }

    /**
     * 更改支付宝账号
     *
     * @param account
     */
    public void changeZfb(String account) {
        if(view!=null){
            view.onWait();
        }
        HttpTool.request(model.changeZfbAccount(account), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onZfbChangeSuccess();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(-3, msg);
                }
            }
        });
    }

    /**
     * 认证
     *
     * @param name
     * @param idCard
     */
    public void updateIdentityInfo(String name, String idCard) {
        if (checkUnLogin()) return;
        HttpTool.request(model.updateIdentityInfo(App.userInfo.getId(), name, idCard), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onBindNameSuccess();
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    public void specifyConfig(int type){
        if (model!=null){
            HttpTool.request(model.specifyConfig(type), new HttpObserver<JSONObject>() {
                @Override
                public void onResult(JSONObject data) {
                    if (view!=null){
                        view.onDisplay(data.getBoolean("alipay"),data.getBoolean("wechat"));
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }

}
